<?php

/**
 * 助手函数类，主要就是解决一些公用的函数所用
 */

namespace app\common\common;

use app\common\model\log\soa2Logs as soa2LogsModel;

/**
 * Description of Helper
 *
 * @author leo.xie
 */
class Helper {

    public static $requestTime = null;

    public static function output($params = [], $msg = 'success', $code = '200', $inPut = [], $apiUserInfo = []) {
        self::addLog(['code' => $code, 'msg' => $msg, 'data' => $params], $inPut, $apiUserInfo);
        \think\Response::create(['code' => '200', 'msg' => $msg, 'data' => $params], 'json')->send();
    }

    public static function outPutError($params, $inPut, $apiUserInfo) {
        self::addLog(['code' => '400', 'msg' => $params['msg'], 'data' => $params], $inPut, $apiUserInfo);
        \think\Response::create(['code' => '400', 'msg' => $params['msg']], 'json')->send();
    }

    private static function addLog($outPut, $inPut, $apiUserInfo) {
        $soa2LogsModel = new soa2LogsModel();
        $soa2LogsModel->addLog($inPut, $outPut, $apiUserInfo);
    }

    /**
     * 手机号码验证
     * @return boolean
     */
    public static function validateMobilePhone($mobilephone) {
        if (preg_match("/^1[0-9]{10}$/", $mobilephone)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 公共下划线转驼峰
     * @param type $arr
     * @return boolean
     */
    public static function convertUnderlineArray($arr = []) {
        if (empty($arr)) {
            [];
        }
        $newArray = [];
        foreach ($arr as $key => $value) {
            $str = ucwords(str_replace('_', ' ', $key));
            $str = str_replace(' ', '', lcfirst($str));
            $str = $ucfirst ? ucfirst($str) : $str;
            $newArray[$str] = (string) $value;
            //如果是数组重构这个数组用递归的方法
            if (is_array($value)) {
                unset($newArray[$str]);
                $newArray[$str] = self::convertUnderlineArray($value);
            }
        }
        return $newArray;
    }

    /**
     * Curl提交数据
     * @param type $url
     * @param type $str
     * @return type
     * @throws \Exception
     */
    public static function HttpPostJson($url, $str, $type,$timeOut = 10) {
        $url = str_replace(' ', '', $url);
        if (!(strpos($url, 'http://') === 0)) {
            $url = 'http://' . $url;
        }

        $ch = curl_init();

        if ($type == 'json') {
            curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
        } else {
            curl_setopt($ch, CURLOPT_POST, 1);
        }

        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_POSTFIELDS, $str);
        curl_setopt($ch, CURLOPT_TIMEOUT,$timeOut);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $tmpInfo = curl_exec($ch);
        if (curl_errno($ch)) {
            throw new \Exception("curl server error" . curl_error($ch));
        }
        curl_close($ch);
        return $tmpInfo;
    }

    /**
     * 驼峰转下划线(暂时只支持一维数组)
     * @return $array
     */
    public static function toUnderScore($arr) {
        if (!is_array($arr)) {
            return false;
        }
        $newArray = [];
        foreach ($arr as $key => $value) {
            $newKey = strtolower(preg_replace('/((?<=[a-z])(?=[A-Z]))/', '_', $key));
            $newArray[$newKey] = $value;
            if (is_array($value)) {
                unset($newArray[$newKey]);
                $newArray[$newKey] = self::toUnderScore($value);
            }
        }
        return $newArray;
    }

    /**
     * 生成订单编号
     * @return type
     */
    public static function createNumber() {
        return date('Ymd') . substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 12);
    }

}
